<?php

namespace app\api\controller\admin\hongruan;

use app\common\controller\AdminApi;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\HrCode;
use fast\Random;
use think\Validate;

/**
 * 虹软激活码
 *
 * @icon fa fa-user
 */
class Code extends AdminApi
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @ApiTitle    (激活码列表)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/2/26)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/admin/hongruan/code/index)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的 Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "",
    "time": "1582701189",
    "data": [
    {
    "id": 1,
    "code": "1234567",
    "device": "admin123",
    "brand": "123",
    "admin_id": 1,
    "school_id": 36,
    "status": 1,
    "activated_at": 1582622536,
    "created_at": 1582533812
    },
    {
    "id": 2,
    "code": "2345678",
    "device": null,
    "brand": null,
    "admin_id": 1,
    "school_id": null,
    "status": 0,
    "activated_at": null,
    "created_at": 1582533812
    },
    {
    "id": 3,
    "code": "3456789",
    "device": null,
    "brand": null,
    "admin_id": 1,
    "school_id": null,
    "status": 0,
    "activated_at": null,
    "created_at": 1582533812
    }
    ]
    })
     */
    public function index()
    {
        $admin_id = $this->auth->id;

        $list = HrCode::where('admin_id', $admin_id)->select();

        $this->success('', $list);
    }

    /**
     * @ApiTitle    (激活)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/2/26)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/admin/hongruan/code/activate)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的 Token")
     * @ApiParams   (name="code", type="string", required=true, description="激活码", sample="1234567")
     * @ApiParams   (name="device", type="string", required=true, description="设备", sample="")
     * @ApiParams   (name="brand", type="string", required=true, description="品牌", sample="")
     * @ApiParams   (name="school_id", type="int", required=true, description="学校", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ()
     */
    public function activate()
    {
        $code = $this->request->request('code'); // 激活码
        $device = $this->request->request('device');// 设备
        $brand = $this->request->request('brand');// 品牌
        $school_id = $this->request->request('school_id');// 学校

        $rule = [
            'code' => 'require',
            'device' => 'require',
            'brand' => 'require',
            'school_id' => 'require',
        ];

        $validate = new Validate($rule);
        $result = $validate->check(compact('code', 'device', 'brand', 'school_id'));
        if (!$result) {
            $this->error($validate->getError());
        }

        $hr_code = HrCode::where('code', $code)->where('admin_id', $this->auth->id)->find();
        if (!$hr_code) {
            $this->error('激活码错误');
        }

        if ($hr_code->status != HrCode::NOT_ACTIVATE) {
            $this->error('激活码已被使用');
        }

        $hr_code->device = $device;
        $hr_code->brand = $brand;
        $hr_code->school_id = $school_id;
        $hr_code->device = $device;
        $hr_code->activated_at = time();
        $hr_code->status = HrCode::ACTIVATE;

        $flag = $hr_code->save();

        if ($flag) {
            $this->success();
        } else {
            $this->error('激活失败');
        }

    }


}
